package com.runa.rccprotocol.analysis.enleman;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.dto.DtuReport;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.protocol.util.DigitalUtil;
import com.runa.rccprotocol.analysis.DaoHengAnalysis;
import com.runa.rccprotocol.dto.equdata.HeatMeterData;
import com.runa.rccprotocol.dto.equdata.TerminalCalendarClock;
import com.runa.rccprotocol.dto.runa.RunaConcenAffirmDeny;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EnLeManAnalysis extends ShellBufferAnalysis<byte[]> {


    public EnLeManAnalysis() {
        super(null, 0);
    }

    @Override
    public int fill(byte[] data, int size,String n) {
        int begin;
        boolean findBegin = false;
        for (begin = 0; begin < size; begin++) {
            int i = data[begin] & 0xff;
            if (i == 0x7E) {
                findBegin = true;
                break;
            }
        }
        if (!findBegin) {
            return 0;
        }
        //先 对应用数据和CRC16校验码进行字符反转义   0x7E	   0x7D  0x5E
        //        //                               0x7D	   0x7D  0x5D
        String dataS = new String(data);
        byte[] dataNew = ByteUtil.hexStringToByte(dataS.replace("7D5E", "7E").replace("7D5D", "7D"));
        int deviceType = dataNew[begin + 1] & 0xff;
        switch (deviceType) {
            case 0x0D:
                //注册包或者心跳包
                byte[] bytes = new byte[dataNew.length - 4];
                System.arraycopy(dataNew, 1, bytes, 0, dataNew.length - 4);
                int checkValue = DigitalUtil.CRC16_XMODEM(bytes);
                Integer value = ByteUtil.byteToUShort(dataNew, 13, ByteOrder.BA);
                if (checkValue != value) {
                    return 0;
                }
                //待定  编号不太对 todo
                String no = new String(dataNew, 2, 8);
                netEquNo = no;
                interData = new DtuReport(no);
            case 0x02:
                HotMeterVO meterData = new HotMeterVO();
                handleMeterData(data, begin, meterData);
            default:
        }
        return 0;
    }

    private void handleMeterData(byte[] data, int index, HotMeterVO meterData) {
        index += 7;
        //集中器时间  使用热表时间
        index += 4;
        index += 2;
        String serialNo = ByteUtil.bcd2str(data, index, 7, false);//待确认
        index += 7;
        meterData.setMeterSerial(serialNo);

        int ctrl = data[index++] & 0xff;
        int DI = ((data[index++] & 0xff) << 8) | data[index++];
        int ser = data[index++];
        if (ctrl != 0x81 || DI != 0x901F) {
            interData = new RunaConcenAffirmDeny(serialNo, false);
            return;
        }

        //结算日热量
        ++index;
        double theCurrentHeat = ByteUtil.bcdToInt(data, index, 4, false) * 0.01;
        index+=4;
        //当前热量(总热量).
        ++index;
        double theCurrentHeatSum = ByteUtil.bcdToInt(data, index, 4, false) * 0.01;
        meterData.setAccHeat((float) theCurrentHeatSum);
        index+=4;
        //热功率
        ++index;
        double thermalPower = ByteUtil.bcdToInt(data, index, 4, false) * 0.01;
        meterData.setPower( (float) thermalPower);
        index+=4;
        //瞬时流量
        ++index;
        double instantaneousDelivery = ByteUtil.bcdToInt(data, index, 4, false) * 0.0001;
        meterData.setFlow( (float) instantaneousDelivery);
        index+=4;
        //累计流量
        ++index;
        double cumulativeDischarge = ByteUtil.bcdToInt(data, index, 4, false) * 0.01;
        meterData.setAccFlow( (float) cumulativeDischarge);
        index+=4;
        //进水温
        double intoTheWater = ByteUtil.bcdToInt(data, index, 3, false) * 0.01;
        meterData.setSupplyTemp( (float) intoTheWater);
        index+=3;
        //回水温度
        double returnWaterTemperature = ByteUtil.bcdToInt(data, index, 4, false) * 0.01;
        meterData.setReturnTemp( (float) returnWaterTemperature);
        index+=3;

        //无单位,累计工作时间
        int openTime = ByteUtil.bcdToInt(data, index, 3, false);
        meterData.setWorkTime(openTime);
        index+=3;
        try {
            meterData.setMeterDate((new SimpleDateFormat("yyyyMMddHHmmss").parse(ByteUtil.bcd2str(data, index, 7, false))));
        } catch (ParseException e) {
            
        }
        meterData.setReadDate(new Date());
        RunaConcenResponseData responseData = new RunaConcenResponseData();
        responseData.addRepData(meterData);
        interData = responseData;
    }

    public static void main(String[] args) {
        EnLeManAnalysis analysis = new EnLeManAnalysis();
//        String s = "68 95 62 07 16 08 21 68 91 08 33 33 33 33 45 33 33 33 50 16".replace(" ","");
        String heart = "7E 0D 68 34 20 11 10 0F 1E 07 12 A5 5A D8 8D 7E".replace(" ", "");
        String heatMeter = "7E 02 02 01 01 01 01 16 23 0D AB 68 20 94 12 61 21 00 00 00 81 2E 90 1F 00 05 66 10 00 00 05 66 10 00 00 17 00 00 00 00 35 00 00 00 00 2C 26 00 00 00 79 27 00 28 27 00 00 00 00 00 31 17 26 08 17 20 00 00 3E 16 42 33 7E".replace(" ", "");
        byte[] bytes = ByteUtil.hexStringToByte(heatMeter);
//        byte[] bytes = s.getBytes();
        analysis.fill(bytes, bytes.length,"");
        analysis.gain();

//        int i = DigitalUtil.calcCrc16(ByteUtil.hexStringToByte("7E 0D 68 34 20 11 10 0F 1E 07 12 A5 5A D8 8D 7E".replace(" ","")), 1, 12);
//        int i = DigitalUtil.CRC16_XMODEM(ByteUtil.hexStringToByte("0D 68 34 20 11 10 0F 1E 07 12 A5 5A"));
//        System.out.println(i);
    }
}
